package com.zryc.shop.pms.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.base.Result;
import com.zryc.shop.pms.entity.ProductAttribute;
import com.zryc.shop.pms.entity.ProductAttributeCategory;
import com.zryc.shop.pms.entity.ProductAttributeValue;
import com.zryc.shop.pms.mapper.ProductAttributeMapper;
import com.zryc.shop.pms.service.IProductAttributeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zryc.shop.pms.service.IProductAttributeValueService;
import com.zryc.shop.ums.entity.Admin;
import com.zryc.shop.ums.entity.Menu;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品属性参数表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@Service
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeMapper, ProductAttribute> implements IProductAttributeService {

    @Autowired
    private IProductAttributeValueService productAttributeValueService;
    /**
     * 查询商品属性列表
     *
     * @param id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<ProductAttribute> list(Integer id, Integer type, Integer pageNum, Integer pageSize) {
        Page<ProductAttribute> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductAttribute> wrapper = new QueryWrapper<>();
        wrapper.eq("product_attribute_category_id", id);
        wrapper.eq("type", type);
        return page(page, wrapper);
    }



    /**
     * 新增商品类型
     *
     * @param productAttribute
     * @return
     */
    @Override
    public Object create(ProductAttribute productAttribute) {
        super.save(productAttribute);
        Long productAttributeCategoryId = productAttribute.getProductAttributeCategoryId();
        Integer type = productAttribute.getType();
        baseMapper.updateAttributeCategory(productAttributeCategoryId, type);
        return "添加成功";
    }

    /**
     * 修改属性
     *
     * @param id
     * @param productAttribute
     * @return
     */
    @Override
    public Object updateDetail(Long id, ProductAttribute productAttribute) {
        UpdateWrapper<ProductAttribute> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        baseMapper.update(productAttribute,updateWrapper);
        return null;
    }

    /**
     * 批量删除属性
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Object delete(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return Result.error(666, "删除的属性ID不能为空");
        }
        // 将逗号分隔的ID字符串转换为Long数组
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
        // 遍历删除的属性，更新 pms_product_attribute_category 表中的字段
        for (Long id : idList) {
            System.out.println();
            ProductAttribute attribute = baseMapper.selectById(id);
            if (attribute != null) {
                Long productAttributeCategoryId = attribute.getProductAttributeCategoryId();
                Integer type = attribute.getType();
                baseMapper.AttributeCategory(productAttributeCategoryId, type);
            }
        }
        removeByIds(idList);
        productAttributeValueService.removeByIds(idList);
        return Result.ok("属性删除成功");
    }

}
